package org.example.multiThreading.synchronous;

public class Synchronized {
    public static void main(String[] args) {
        //四个窗口
//        TestSynchronized ticketWin1=new TestSynchronized("窗口1");
//        TestSynchronized ticketWin2=new TestSynchronized("窗口2");
//        TestSynchronized ticketWin3=new TestSynchronized("窗口3");
//        TestSynchronized ticketWin4=new TestSynchronized("窗口4");
//        ticketWin1.start();
//        ticketWin2.start();
//        ticketWin3.start();
//        ticketWin4.start();

        TestSynchronized2 ticketWin1=new TestSynchronized2("窗口1");
        TestSynchronized2 ticketWin2=new TestSynchronized2("窗口2");
        TestSynchronized2 ticketWin3=new TestSynchronized2("窗口3");
        TestSynchronized2 ticketWin4=new TestSynchronized2("窗口4");
        ticketWin1.start();
        ticketWin2.start();
        ticketWin3.start();
        ticketWin4.start();
    }
}

class TestSynchronized extends Thread{

    public TestSynchronized(String name){
        super(name);
    }

    public static Integer ticket = 100;

    @Override
    public void run(){
        while (true)
        {
            synchronized (TestSynchronized.ticket) {
                if(TestSynchronized.ticket<=0){
                    break;
                }
                System.out.println(Thread.currentThread().getName()+"卖出了一张票，还剩"+(--TestSynchronized.ticket)+"张。");
            }
        }
    }
}

class TestSynchronized2 extends Thread{

    public TestSynchronized2(String name){
        super(name);
    }

    public static Integer ticket = 100;

    @Override
    public synchronized void run(){
        while (TestSynchronized.ticket > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出了一张票，还剩" + (--TestSynchronized.ticket) + "张。");
        }
    }
}